package com.sx.pipeline.ui.examine.record

import android.app.Application
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.config.BaseEntity
import com.sx.pipeline.app.config.ReleaseTaskType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.res.ExamineOperatorVo
import com.sx.pipeline.repos.http.res.ExamineRecordVo
import com.sx.pipeline.repos.http.res.Ipage

class ExamineRecordVM(app: Application) : CYBaseViewModel(app)  {

    fun getExamineTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_pending_review))
        tabs.add(getApplication<Application>().getString(R.string.txt_passed))
        tabs.add(getApplication<Application>().getString(R.string.txt_rejected))
        tabs.add(getApplication<Application>().getString(R.string.txt_cancelled))
        return tabs
    }

    fun getExamineTaskTabsIdList(): List<Int> {
        val tabs = mutableListOf<Int>()
        tabs.add(0)
        tabs.add(1)
        tabs.add(2)
        tabs.add(3)
        return tabs
    }


    val examineListEvent: SingleLiveEvent<Ipage<ExamineRecordVo>> = SingleLiveEvent()
    var refreshEvent = SingleLiveEvent<Boolean>()

    fun getExamineList(page: Int, status: Int) {
        addSubscribe(mModel.getExamineRecordList(status, page)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Ipage<ExamineRecordVo>, BaseViewModel<*>>(this, true){
                override fun onResult(result: Ipage<ExamineRecordVo>) {

                    examineListEvent.postValue(result)
                    if (page == 1) {
                        mUI.initRefreshEvent().postValue(true)
                    } else {
                        mUI.initLoadEvent().postValue(true)
                    }
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                    mUI.initRefreshEvent().postValue(false)
                    if (page != 1) {
                        mUI.initLoadEvent().postValue(true)
                    }
                }

            })
        )
    }

    fun cancelApply(item: ExamineRecordVo){
        addSubscribe(mModel.audit(item.id,item.status)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>(this, true){
                override fun onResult(result: Any) {
                    ToastUtils.showShort("已取消")
                    refreshEvent.postValue(true)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            })
        )
    }
}